package cn.wangzg.viewpagersample;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.ArrayList;

/**
 * Time: 2020/3/5
 * Author: wangzhiguo
 * Description: 功能描述
 */
public class MyPagerAdapter extends PagerAdapter {
    private ArrayList<View> viewLists;

    public MyPagerAdapter() {
    }
    //通过构造函数为viewLists赋值
    public MyPagerAdapter(ArrayList<View> viewLists) {
        super();
        this.viewLists = viewLists;
    }
    //要滑动的View的个数
    @Override
    public int getCount() {
        return viewLists.size();
    }

    //判断view和object是否为同一对象，官方建议写法
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }
    //初始化，1.将当前视图添加到container中
    //       2.返回当前View
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(viewLists.get(position));
        return viewLists.get(position);
    }
    //销毁,从当前container中删除指定位置（position）的View
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(viewLists.get(position));
    }
}
